<template>
  <div class="artice-list">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="item in list"
          :key="item.art_id"
          :title="item.title"
          @click="toDetail(item.art_id)"
        >
          <!-- 具名插槽 标题的底部 -->
          <template #label>
            <!-- 主体内容区域 -->
            <van-grid :border="false" :column-num="item.cover.type">
              <van-grid-item
                v-for="(it, index) in item.cover.images"
                :key="index"
              >
                <van-image lazy-load fit="cover" :src="it" />
              </van-grid-item>
            </van-grid>
            <!-- 底部额外信息区域 -->
            <div class="meta">
              <!-- 左侧 -->
              <div class="info-box">
                <span>{{ item.aut_name }}</span>
                <span>{{ item.comm_count }}</span>
                <span>{{ item.pubdate | formatTime }}</span>
              </div>
              <!-- 右侧 -->
              <van-icon
                v-show="$store.state.userInfo.name"
                @click.stop="showAction(item.art_id)"
                name="cross"
              ></van-icon>
            </div>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
    <!-- 弹框 -->
    <van-popup v-model="isShowAction" :style="{ width: '60%' }" @closed="Off">
      <van-cell-group v-if="!isShowTwo">
        <van-cell title="不感兴趣" @click="setDislike" />
        <van-cell title="反馈垃圾内容" is-link @click="isShowTwo = true" />
        <van-cell title="拉黑作者" />
      </van-cell-group>
      <!-- 反馈内容二级弹框 -->
      <van-cell-group v-else>
        <van-cell icon="arrow-left" @click="isShowTwo = false">返回</van-cell>
        <van-cell
          v-for="item in reportList"
          :key="item.type"
          :title="item.title"
          @click="reportArt(item.type)"
        />
      </van-cell-group>
    </van-popup>
  </div>
</template>

<script>
import { getArticles, disLikeArticle, reportArticle } from '../../../api/home'
export default {
  name: 'articleList',
  // 父到子传递参数
  props: {
    channelId: Number
  },
  data () {
    return {
      // 文章列表
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      // 默认数据的时间戳
      timestamp: undefined,
      // 是否显示弹框
      isShowAction: false,
      // 是否显示二级弹框
      isShowTwo: false,
      // 当前执行更多操作的文章id
      target: '',
      // 举报选项
      reportList: [
        { type: '0', title: '其他问题' },
        { type: 1, title: '标题夸张' },
        { type: 2, title: '低俗涩情' },
        { type: 3, title: '错别字多' },
        { type: 4, title: '旧闻重复' },
        { type: 5, title: '广告软文' },
        { type: 6, title: '内容不是' },
        { type: 7, title: '涉嫌违法犯罪' },
        { type: 8, title: '侵权' }
      ]
    }
  },
  methods: {
    // 弹出层优化
    Off () {
      this.isShowAction = false
      this.isShowTwo = false
    },
    // 举报文章
    reportArt (type) {
      reportArticle({ target: this.target, type }).then(res => {
        // 轻提示框
        this.$toast.success('举报成功')
        // 关闭一级弹框
        this.isShowAction = false
        // 关闭二级弹框
        this.isShowTwo = false
      })
    },
    // 不喜欢文章
    setDislike () {
      disLikeArticle({ target: this.target }).then(res => {
        console.log(res)
        this.isShowAction = false
        this.list = this.list.filter(v => {
          if (v.art_id === res.data.data.target) {
            return false
          } else {
            return true
          }
        })
      })
    },
    // 点击转到详情文章
    toDetail (id) {
      this.$router.push({
        path: '/article',
        query: {
          id
        }
      })
    },
    // 点击叉号显示弹框
    showAction (id) {
      this.isShowAction = true
      this.target = id
    },
    onLoad () {
      if (this.timestamp === undefined) {
        this.timestamp = Date.now()
      }
      getArticles({
        timestamp: this.timestamp,
        channel_id: this.channelId
      }).then(res => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        // 保存下一页时间戳
        this.timestamp = res.data.data.pre_timestamp || Date.now()
        // 列表数据
        this.list.push(...res.data.data.results)
        // 关闭loading
        this.loading = false
        // 超过100条
        if (this.list.length >= 100) {
          this.finished = true
        }
      })
      // setTimeout(() => {
      //   if (this.refreshing) {
      //     this.list = []
      //     this.refreshing = false
      //   }
      //   for (let i = 0; i < 10; i++) {
      //     this.list.push(this.list.length + 1)
      //   }
      //   this.loading = false
      //   if (this.list.length >= 40) {
      //     this.finished = true
      //   }
      // }, 1000)
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    }
  }
}
</script>

<style lang="less">
.artice-list {
  .meta {
    display: flex;
    justify-content: space-between;
    span {
      margin-right: 10px;
    }
  }
}
</style>
